/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Estruturas;

import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

public class Manipuladores {

    public static String getIP () //"formato '000.111.222.233"
    {
        try {
            return String.format("%03d.%03d.%03d.%03d", InetAddress.getLocalHost().getAddress()[0], InetAddress.getLocalHost().getAddress()[1],
                    InetAddress.getLocalHost().getAddress()[2], InetAddress.getLocalHost().getAddress()[3]);
        } catch (UnknownHostException ex) {
            System.out.println("Erro ao tomar o IP local!");
            return null;
        }
    }

    public static String getIP (Socket s) //"formato '000.111.222.233"
    {
        InetAddress adrr = s.getInetAddress();
        String address = adrr.getHostAddress();
        StringTokenizer tempStringTokenizer = new StringTokenizer(address,".");

        return String.format("%03d.%03d.%03d.%03d",Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()));
    }


    public static String getIP (InetAddress i) //"formato '000.111.222.233"
    {
        InetAddress adrr = i;
        String address = adrr.getHostAddress();
        StringTokenizer tempStringTokenizer = new StringTokenizer(address,".");

        return String.format("%03d.%03d.%03d.%03d",Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()),
                Integer.parseInt((String) tempStringTokenizer.nextToken()));
    }

    public static byte[] IPtoByteArray (String ip)    //formato '003.023.123.123'
    {
        byte[] retIP = new byte[4];
        StringTokenizer tempStringTokenizer = new StringTokenizer(ip,".");

        retIP = new byte[] {Byte.parseByte((String) tempStringTokenizer.nextToken()),
                Byte.parseByte((String) tempStringTokenizer.nextToken()),
                Byte.parseByte((String) tempStringTokenizer.nextToken()),
                Byte.parseByte((String) tempStringTokenizer.nextToken())};

        //System.out.printf("%s.%c.%c.%c",(char)retIP[0],(char)retIP[1],(char)retIP[2],(char)retIP[3]);
        //(0x000000FF & (int)retIP[0]),(0x000000FF & (int)retIP[1]),
        //(0x000000FF & (int)retIP[2]),(0x000000FF & (int)retIP[3]));
        return retIP;
    }

    public static String extrairDados (byte[] dados, int offset , int end_of_offset)
    {
        try
        {
            String returnString = "";
            int j;
            for(j=offset;j<end_of_offset;j++)
                returnString += dados[j];
            return returnString;

        } catch (IndexOutOfBoundsException e)
        {
            e.printStackTrace();
            return "";
        }
    }
    
    public static int byteArrayToInt(byte[] b, int offset) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public static byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }

    public static short byteArrayToShort(byte[] b, int offset) {
        short value = 0;
        for (int i = 0; i < 2; i++) {
            int shift = (2 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public static byte[] ShortToByteArray(short value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 2; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }




}
